from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class RegisterPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.username_input_locator = (By.ID, "username")
        self.email_input_locator = (By.ID, "email")
        self.password_input_locator = (By.ID, "password")
        self.confirm_password_input_locator = (By.ID, "confirm-password")
        self.register_button_locator = (By.ID, "register-button")
        self.error_message_locator = (By.ID, "error-message")
        self.success_message_locator = (By.ID, "success-message")

    def navigate(self):
        self.driver.get('http://example.com/register')

    def enter_username(self, username):
        username_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.username_input_locator)
        )
        username_input.send_keys(username)

    def enter_email(self, email):
        email_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.email_input_locator)
        )
        email_input.send_keys(email)

    def enter_password(self, password):
        password_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.password_input_locator)
        )
        password_input.send_keys(password)

    def confirm_password(self, confirm_password):
        confirm_password_input = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.confirm_password_input_locator)
        )
        confirm_password_input.send_keys(confirm_password)

    def click_register_button(self):
        register_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.register_button_locator)
        )
        register_button.click()

    def get_error_message(self):
        error_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.error_message_locator)
        )
        return error_message.text

    def get_success_message(self):
        success_message = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.success_message_locator)
        )
        return success_message.text